#include <iostream>
#include <ncurses.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include "testCurses.h"
#include <algorithm>
using namespace std;

void handle(int m){
    cout << "handle child" << endl;
    cout << "befor wait" <<endl;
    wait(NULL);
    cout << "after wait" << endl;
}

void testFork()
{
    int *a = new int[2];
    a[0] = 1;
    a[1] = 0;
    int pid = fork();
    if(pid < 0){
        cout << "fork error\n" <<endl;
    }
    else if (pid == 0) {
        cout << "child: " << a[0] << endl;
        a[0] = 0;
        cout << "child: " << a[0] << endl;
    }
    else{
        signal(SIGCHLD,handle);
        cout << "parent: " << a[0] << endl;
        sleep(2);
        cout << "parent: " << a[0] << endl;
        while(1){
            sleep(1);
            cout << "I am in parent" << endl;
        }
    }
}

int main()
{
     testFork();
    return 0;
}

